//
//  GKZYSelectionListController.m
//  GKZYAid
//
//  Created by lanou3g on 16/2/27.
//  Copyright © 2016年 GKZYAid. All rights reserved.
//

#import "GKZYSelectionListController.h"
#import "DatabaseRequest.h"
#import "Urls.h"
#import "GKZYMessageModel.h"
#import "GKZYSelectionCell.h"
#import "GKZYSelectionDetailController.h"
#import "MJRefresh.h"

@interface GKZYSelectionListController ()<UITableViewDataSource,UITableViewDelegate>
//精选数据数组:
@property (nonatomic,strong)NSMutableArray *selectionDataArray;
//更新数据个数:
@property (nonatomic,assign)NSInteger n;

@end

@implementation GKZYSelectionListController

- (void)loadView{
    
    self.selectionView = [[GKZYSelectionView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.selectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //更新网址条数初值:
    self.n = 0;
    
    //注册cell:
    [self.selectionView.tableView registerClass:[GKZYSelectionCell class] forCellReuseIdentifier:@"SelectionCELL"];
    
    //设置代理:
    [self setDelegate];
    
    //请求数据:
    [self makeData];
    
    //上下拉刷新加载数据:
    [self setupRefresh];
}

- (void)setupRefresh
{
    
    
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.selectionView.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
#warning 自动刷新(一进入程序就下拉刷新)
    [self.selectionView.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.selectionView.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.selectionView.tableView.headerPullToRefreshText = @"下拉可以刷新了";
    self.selectionView.tableView.headerReleaseToRefreshText = @"松开马上刷新了";
    self.selectionView.tableView.headerRefreshingText = @"MJ哥正在帮你刷新中,不客气";
    
    self.selectionView.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.selectionView.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    self.selectionView.tableView.footerRefreshingText = @"MJ哥正在帮你加载中,不客气";
}

//下拉刷新:
- (void)headerRereshing{
    
    if (self.n > 0) {
        
        self.n --;
        
        [self makeData];
        
        [self.selectionView.tableView headerEndRefreshing];
        
    }else{
        
        self.selectionView.tableView.headerRefreshingText = @"已经是最新数据了";
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.selectionView.tableView headerEndRefreshing];
        });
        
        
    }
    
    
}

//上拉加载:
- (void)footerRereshing{
    
    if (self.n < 30) {
        
        self.n ++;
        
        [self makeData];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.selectionView.tableView footerEndRefreshing];
            
        });
        
        
    }else{
        
        self.selectionView.tableView.footerRefreshingText = @"MJ哥正在帮你加载中,不客气";
    }
    
    
    
}

//懒加载初始化:
- (NSMutableArray *)selectionDataArray{
    
    if (!_selectionDataArray) {
        
        _selectionDataArray = [[NSMutableArray alloc]init];
        
    }
    
    return _selectionDataArray;
}

- (void)makeData{
    
    [DatabaseRequest requestMessageUrlString:[NSString stringWithFormat:KSelection,self.n * 30] didFished:^(NSData *data) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableDictionary *dicRes = dict[@"res"];
        NSMutableArray *arrayNewsList = dicRes[@"news_list"];
        
        if (self.selectionDataArray != nil) {
            
            [self.selectionDataArray removeAllObjects];
        }
        
        for (NSDictionary *dicModel in arrayNewsList) {
            GKZYMessageModel *model = [[GKZYMessageModel alloc] init];
            [model setValuesForKeysWithDictionary:dicModel];
            [self.selectionDataArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.selectionView.tableView reloadData];
        });
    }];
    
}

- (void)setDelegate{
    
    self.selectionView.tableView.delegate = self;
    self.selectionView.tableView.dataSource = self;
}

#pragma mark---------实现UITableView代理方法--------------
//返回多少个分区:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

//返回分区下的行数:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.selectionDataArray.count;
}

//设置cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GKZYSelectionCell *cell = [self.selectionView.tableView dequeueReusableCellWithIdentifier:@"SelectionCELL"];
    
    cell.model = self.selectionDataArray[indexPath.row];
    
    return cell;
}

//设置行高:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120;
}

//点击cell跳转:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GKZYSelectionDetailController *selectionDetailVC = [GKZYSelectionDetailController new];
    selectionDetailVC.modelDetail = self.selectionDataArray[indexPath.row];
    
    [self.navigationController pushViewController:selectionDetailVC animated:YES];
    
}

//返回分区头视图的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

//返回分区尾部视图的高度;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
